﻿Imports System.Data.SqlClient
Public Class members_productsubmit
    Inherits System.Web.UI.Page
    Private sSQL As String
    Private objrd As SqlDataReader = Nothing
    Private objfind As New GetData
    Private ps As New PreparedStatement(sSQL)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Session("uid") <> Nothing And Session("uid") <> "") Then
            Dim productName, priceUnit, specification, brand, location, productCategory, productSubCategory, description As String
            Dim memberId, strErr As String
            Dim companyId As String = ""
            Dim productId, fileFullName, fileName, photoPath As String
            Dim productType As Boolean
            Dim price
            strErr = ""
            memberId = Session("uid")

            If (Request("submitproduct") <> Nothing And Request("submitproduct") <> "") Then
                If (Request("c_product_name") = "" Or Request("c_product_name") = Nothing) Then
                    Me.c_product_name_label.Text = "请输入产品名称！"
                    Exit Sub
                Else
                    Me.c_product_name_label.Text = ""
                End If
                If (Request("c_product_category") = "" Or Request("c_product_category") = Nothing) Then
                    Me.c_category_label.Text = "请输入产品分类！"
                    Exit Sub
                Else
                    Me.c_category_label.Text = ""
                End If
                If (Request("c_description") = "" Or Request("c_description") = Nothing) Then
                    Me.c_description_label.Text = "请输入详细描述！"
                    Exit Sub
                Else
                    Me.c_description_label.Text = ""
                End If
                ps.strSql = "select * from merchant where owner_id =@ownerId"
                ps.addParameter("ownerId", memberId)
                Dim conn As SqlConnection = objfind.getConn()

                objrd = objfind.dr(ps, strErr, conn, objrd)
                If (objrd.Read()) Then
                    companyId = objrd.Item("company_id")
                End If
                If (Request("c_productType") = "1") Then
                    productType = True
                ElseIf (Request("c_productType") = "0") Then
                    productType = False
                End If
                productName = Request("c_product_name")
                If (Request("c_price") <> Nothing And Request("c_price") <> "") Then
                    price = Request("c_price")
                Else
                    price = "0"
                End If
                priceUnit = Request("c_price_unit")
                specification = Request("c_specification")
                brand = Request("c_brand")
                location = Request("c_location")
                productCategory = Request("c_product_category")
                If (Request("c_product_subcategory") <> Nothing And Request("c_product_subcategory") <> "") Then
                    productSubCategory = Request("c_product_subcategory")
                Else
                    productSubCategory = ""
                End If
                description = Request("c_description")
                productId = System.Guid.NewGuid().ToString()
                ps.strSql = "insert into product_info (product_id,company_id,product_type,product_name,price,price_unit,specification,brand," & _
                            "location,category_id,sub_category_id,recommendation,active,description,lang_flag,create_date) values(@productId,@companyId,@productType,@productName," & _
                            "@price,@priceUnit,@specification,@brand,@location,@productCategory,@productSubCategory,@recommendation,@active,@description,'zh',@createDate)"
                ps.parameters.Clear()
                ps.addParameter("productId", productId)
                ps.addParameter("companyId", companyId)
                ps.addParameter("productType", productType)
                ps.addParameter("productName", productName)
                ps.addParameter("price", price)
                ps.addParameter("priceUnit", priceUnit)
                ps.addParameter("specification", specification)
                ps.addParameter("brand", brand)
                ps.addParameter("location", location)
                ps.addParameter("productCategory", productCategory)
                ps.addParameter("productSubCategory", productSubCategory)
                ps.addParameter("recommendation", False)
                ps.addParameter("active", True)
                ps.addParameter("description", description)
                ps.addParameter("createDate", Now)
                objfind.es(ps, strErr)
                If (image.PostedFile.FileName <> Nothing) Then
                    fileFullName = image.PostedFile.FileName
                    fileName = fileFullName.Substring(fileFullName.LastIndexOf("\\") + 1)
                    System.IO.Directory.CreateDirectory(Server.MapPath("../../") & "pic/product/" & productId)
                    image.PostedFile.SaveAs(Server.MapPath("../../") & "pic/product/" & productId & "/" & fileName)
                    photoPath = "/pic/product/" & productId & "/" & fileName
                    ps.strSql = "update product_info set photo_path = @photoPath where product_id=@productId"
                    ps.parameters.Clear()
                    ps.addParameter("photoPath", photoPath)
                    ps.addParameter("productId", productId)
                    objfind.es(ps, strErr)
                    Me.product_id.Value = productId
                    imageDiv.InnerHtml = "<img src=""" & photoPath & """ width=""100"" height=""100"" />"
                End If
                objfind.closeConn(conn)

                Response.Redirect("members-productsubmitpic.aspx?productId=" & productId)

            Else
                ps.strSql = "select * from category where lang_flag='zh'"
                ps.parameters.Clear()
                Dim conn As SqlConnection = objfind.getConn()

                objrd = objfind.dr(ps, strErr, conn, objrd)
                While (objrd.Read())
                    c_product_category.Items.Add(New ListItem(
                    objrd("Category_Name").ToString(), objrd("Category_ID").ToString()))
                End While
                objfind.closeConn(conn)

            End If
        Else
            Response.Redirect("../cn_login.aspx")
        End If
    End Sub

    Protected Sub c_product_category_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles c_product_category.SelectedIndexChanged
        Dim categoryId, strErr As String
        strErr = ""
        categoryId = c_product_category.SelectedValue
        ps.strSql = "select * from sub_category where category_id=@categoryId and lang_flag='zh'"
        ps.parameters.Clear()
        ps.addParameter("categoryId", categoryId)
        Dim conn As SqlConnection = objfind.getConn()

        objrd = objfind.dr(ps, strErr, conn, objrd)
        c_product_subcategory.Items.Clear()
        c_product_subcategory.Items.Add(New ListItem("请选择二级分类", ""))
        While (objrd.Read())
            c_product_subcategory.Items.Add(New ListItem(
            objrd("Sub_Category_Name").ToString(), objrd("Sub_Category_ID").ToString()))
        End While
        c_product_category.Items.Clear()
        c_product_category.Items.Add(New ListItem("请选择一级分类", ""))
        ps.strSql = "select * from category where lang_flag='zh'"
        ps.parameters.Clear()
        objrd = objfind.dr(ps, strErr, conn, objrd)
        While (objrd.Read())
            c_product_category.Items.Add(New ListItem(
            objrd("Category_Name").ToString(), objrd("Category_ID").ToString()))
        End While
        objfind.closeConn(conn)

        c_product_category.SelectedValue = categoryId
    End Sub

End Class